package com.javaEE.tes.controllers;

import com.javaEE.tes.entities.Teacher;
import com.javaEE.tes.services.Class1Service;
import com.javaEE.tes.services.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.validation.Valid;
import java.util.List;

@Controller
@RequestMapping("teacher")
public class TeacherControllers {
    @Autowired
    TeacherService teacherService;
    @Autowired
    Class1Service class1Service;
    @GetMapping
    public String displayTeacher(Model model){
        List<Teacher> teachers = teacherService.getAll();
        model.addAttribute("teacher",teachers);
        return "teacher/list-teacher";
    }

    @GetMapping("/new")
    public String displayTeacherFrom(Model model){

        Teacher aTeacher = new Teacher();
        model.addAttribute("teacher",aTeacher);
        return "teacher/new-teacher";
    }
    @PostMapping("/save")
    public  String createTeacher(Model model, @Valid Teacher teacher, Errors errors){
        if (errors.hasErrors()) return "teacher/new-teacher";
        teacherService.save(teacher);
        return "redirect:/teacher";
    }

}
